package club.kingyin.commons.response;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;

import java.io.Serializable;

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Result implements Serializable {
    private String code;

    private String message;

    private Object data;

    private boolean success;


    public boolean isSuccess() {
        return !code.equals(ResultCode.FAILURE_NOTIFY.val()) &&
                !code.equals(ResultCode.FAILURE_REPORT.val()) &&
                !code.equals(ResultCode.ERROR.val());
    }

    public static Result success(Object data) {
        return resultData(ResultCode.SUCCESS.val(), ResultCode.SUCCESS.msg(), data, true);
    }

    public static Result success(Object data, String msg) {
        return resultData(ResultCode.SUCCESS.val(), msg, data,true);
    }

    public static Result fail(String code, String msg) {
        return resultData(code, msg, null, false);
    }

    public static Result fail(String code, String msg, Object data) {
        return resultData(code, msg, data, false);
    }

    public static Result infoN(String msg) {
        return resultData(ResultCode.INFO_NOTIFY.val(), msg, null,true);
    }

    public static Result infoR(String msg) {
        return resultData(ResultCode.INFO_REPORT.val(), msg, null,true);
    }

    public static Result waringN(String msg) {
        return resultData(ResultCode.WARING_NOTIFY.val(), msg, null,true);
    }

    public static Result waringR(String msg) {
        return resultData(ResultCode.WARING_REPORT.val(), msg, null,true);
    }

    public static Result failN(String msg) {
        return resultData(ResultCode.FAILURE_NOTIFY.val(), msg, null, false);
    }

    public static Result failR(String msg) {
        return resultData(ResultCode.FAILURE_REPORT.val(), msg, null,false);
    }

    private static Result resultData(String code, String msg, Object data, boolean success) {
        Result resultData = new Result();
        resultData.setCode(code);
        resultData.setMessage(msg);
        resultData.setData(data);
        resultData.setSuccess(success);
        return resultData;
    }
}
